<#import "/global.ftl" as role_list />
<@role_list.importEasyUi/>
<form id="roleQueryForm">
    <table>
        <tr>
            <td><label for="sysRoleQuery.roleName" style=" font-size: 12px;">角色名字:</label></td>
            <td>
                <input class="easyui-validatebox" type="text" id="roleName" name="sysRoleQuery.roleName" value=""/>
            </td>
            <td>
                <input type="button" value="Search" id="searchBtn" onclick="searchClick()" />
            </td>
            <td>&nbsp;</td>
        </tr>
    </table>
</form>

<table id="roleTables"  style="width:100%;height:500px"
</table>
<div id="addRoleDiv" data-options="iconCls:'icon-save',resizable:true,modal:true"></div>
<script type="text/javascript">
    var pageType = "add";
    var pageName = "edit";
    var selectRow;


    function searchClick(){
        $("#roleTables").datagrid('load',{"sysRoleQuery.roleName":$('#roleName').val()});
    }

    $(function(){
        $("#roleTables").datagrid({
            url : '<@role_list.basePath/>/sys/role/list',
            method: 'post',
            pageSize : 10,
            pageList : [10, 20, 30, 40, 50],
            border : true,
            pagination:true,
            idField : 'roleId',
            rownumbers: true,
            singleSelect:true,
            fitColumns:true,
            loadMsg:'数据正在努力加载，请稍后...',
            columns : [[
                { field: 'roleId', title: 'roleId', width: 100},
                { field: 'roleName', title: 'roleName', width: 100},
                { field: 'roleOffice', title: 'roleOffice', width: 100},
                { field: 'isSystem', title: 'isSystem', width: 100},
                { field: 'status', title: 'status', width: 100},
                { field: 'createTime', title: 'createTime', width: 100},
                { field: 'createBy', title: 'createBy', width: 100},
                { field: 'updateTime', title: 'updateTime', width: 100},
                { field: 'updateBy', title: 'updateBy', width: 100},
                { field: 'remarks', title: 'remarks', width: 100}
            ]],
            toolbar: [
            <@hasPermissionTag name="sysuser:role:add">
                { iconCls: 'icon-add',      handler: function(){ selectRow={"roleId":''};pageName = "edit"; pageType = "add"; editRole(); }}, '-',
            </@hasPermissionTag>
            <@hasPermissionTag name="sysuser:role:edit">
                { iconCls: 'icon-edit',     handler: function(){
                    selectRow = getSelectedRows();
                    console.log(selectRow);
                    if(selectRow==undefined){
                        $.messager.alert('Info','请选择要修改的行');
                        return ;
                    }
                    pageName = "edit";
                    pageType = "edit";
                    editRole();
                }}, '-',
            </@hasPermissionTag>
            <@hasPermissionTag name="sysuser:role:delete">
                { iconCls: 'icon-cancel',   handler: function(){
                    $.messager.confirm('Confirm','Are you sure you want to delete record?',function(r){
                        if(r){
                            roleDelete();
                        }
                    });
                }}, '-',
            </@hasPermissionTag>
                { iconCls: 'icon-search',   handler: function(){
                    selectRow = getSelectedRows();
                    console.log(selectRow);
                    if(selectRow==undefined){
                        $.messager.alert('Info','请选择要修改的行');
                        return ;
                    }
                    pageName = "view";
                    pageType = "view";
                    viewMenu();
                }}

            ]
        });
    });

    function getSelectedRows(){
        return $("#roleTables").datagrid('getSelected');
    }

    function roleDelete(){
        selectRow = getSelectedRows();
        console.log(selectRow);
        if(selectRow==undefined){
            $.messager.alert('Info','请选择要删除的行');
            return ;
        }
        $.post('<@role_list.basePath/>/sys/role/delete',
                {"roleId": selectRow['roleId']},
                function(data){
                    if(true==data.success){
                        $.messager.alert('Info','删除成功!');
                        $("#roleTables").datagrid('reload');
                    }
                });
    }

    function editRole(){
        $('#addRoleDiv').dialog({
            title: 'Role '+pageType,
            width: 400,
            height: 350,
            closed: false,
            cache: false,
            href: '<@role_list.basePath/>/sys/role/edit',
            queryParams: {"pageType":pageType,"roleId":selectRow['roleId'] },
            modal: true,
            iconCls:'icon-edit',
            buttons:[
                { text: 'Save', handler: function () { roleEditSubmit(); }},
                { text: 'Close', handler: function () { $('#addRoleDiv').dialog('close'); }}
            ]
        });
    }

    function viewMenu() {
        $('#addRoleDiv').dialog({
            title: 'Role ' + pageType,
            width: 400,
            height: 350,
            closed: false,
            cache: false,
            href: '<@role_list.basePath/>/sys/role/view',
            queryParams: {"pageType": pageType, "roleId": selectRow['roleId']},
            modal: true,
            iconCls: 'icon-search',
            buttons: [
                { text: 'Close', handler: function () { $('#addRoleDiv').dialog('close'); }}
            ]
        });
    }
</script>